# -*- coding: utf-8 -*-
# @Time   : 2023/9/18 14:01
# @Author : 大龙 🚀
# @File   : shenzhen_illegal.py
import re

from apps.tax_illegal.tax_illegal.items import NetTaxIllegalProItem
from apps.tax_illegal.tax_illegal.spiders import BaseTaxIllegalSpider


class ShenzhenIllegalSpider(BaseTaxIllegalSpider):
    name = "shenzhen_illegal"
    province: str = "深圳"
    url: str = "https://shenzhen.chinatax.gov.cn/sztaxapp/zdsswfaj/index#"
    index_token = "7AE4041B3BED018087D7F562B48BD0C2"
    page_token = "7AE4041B3BED018087D7F562B48BD0C2"
    time = ""

    def start_requests(self):
        url = "https://shenzhen.chinatax.gov.cn/sztaxapp/zdsswfaj/index"
        yield self.Request(url, callback=self.parse)
        yield from self.etax_search(**{"etax_url": "https://etax.shenzhen.chinatax.gov.cn:8443"})

    def parse_list(self, response, **kwargs):
        ...
        # self.page_token = re.findall(".value='(.*?)';", response.text)[0]  # 翻页token
        # yield from self.parse(response, **kwargs)
        # pages = 3
        # if pages and int(pages) > 1:
        #     for page in range(2, 4):
        #         url = "https://shenzhen.chinatax.gov.cn/sztaxapp/zdsswfaj/list"
        #         data = {"curPage": str(page), "totalPages": "15", "token": self.page_token, "pageNum": "1"}
        #         print("page_token: ", self.page_token)
        #         headers_page = {
        #             "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
        #             "Accept-Language": "zh-CN,zh;q=0.9",
        #             "Cache-Control": "max-age=0",
        #             "Connection": "keep-alive",
        #             "Content-Type": "application/x-www-form-urlencoded",
        #             "$Cookie": "yfx_c_g_u_id_10003701=_ck23051013442518234376127156743; yfx_mr_10003701=%3A%3Amarket_type_free_search%3A%3A%3A%3Abaidu%3A%3A%3A%3A%3A%3A%3A%3Awww.baidu.com%3A%3A%3A%3Apmf_from_free_search; yfx_key_10003701=; yfx_c_g_u_id_10003175=_ck23053020263012072817311349191; yfx_c_g_u_id_10003717=_ck23072116032013065718137255227; _trs_uv=lkcany97_343_ck8y; yfx_f_l_v_t_10003175=f_t_1685449590199__r_t_1690533252565__v_t_1690533252565__r_c_1; yfx_c_g_u_id_10003708=_ck23080816463917211748761136237; yfx_c_g_u_id_10000056=_ck23080816470115560933714246336; yfx_f_l_v_t_10000056=f_t_1691484421554__r_t_1691484421554__v_t_1691484421554__r_c_0; yfx_c_g_u_id_10000001=_ck23082317243517831777563655695; yfx_c_g_u_id_10003677=_ck23082317243518073277457075058; yfx_c_g_u_id_10003710=_ck23082915212314104555018597717; yfx_c_g_u_id_10000447=_ck23091310350717509677699837177; yfx_f_l_v_t_10000447=f_t_1694572507745__r_t_1694572507745__v_t_1694572507745__r_c_0; yfx_c_g_u_id_10001429=_ck23091310390613727245608175332; yfx_f_l_v_t_10001429=f_t_1694572746370__r_t_1694572746370__v_t_1694572746370__r_c_0; yfx_c_g_u_id_10003747=_ck23091314592219824001008493050; yfx_c_g_u_id_10003705=_ck23091414082717949418370155575; yfx_f_l_v_t_10003705=f_t_1694671707792__r_t_1694671707792__v_t_1694671707792__r_c_0; yfx_c_g_u_id_10002888=_ck23091414570016733246231383657; yfx_f_l_v_t_10002888=f_t_1694674620670__r_t_1694674620670__v_t_1694674620670__r_c_0; yfx_f_l_v_t_10003747=f_t_1694588362983__r_t_1694670677738__v_t_1694675781287__r_c_1; _va_id=470ece5613c17add.1694588363.4.1694675795.1694656958.; yfx_c_g_u_id_10003714=_ck23091416122010673472253495353; yfx_f_l_v_t_10003710=f_t_1693293683402__r_t_1694656348181__v_t_1694679177000__r_c_2; yfx_c_g_u_id_10003715=_ck23091416131216721759447304278; yfx_f_l_v_t_10003715=f_t_1694679192670__r_t_1694745468635__v_t_1694745468635__r_c_1; yfx_f_l_v_t_10000001=f_t_1692782675781__r_t_1694760968382__v_t_1694760968382__r_c_1; yfx_f_l_v_t_10003677=f_t_1692782675799__r_t_1694760968402__v_t_1694760968402__r_c_1; yfx_c_g_u_id_10003746=_ck23091514585711943518151458783; yfx_f_l_v_t_10003746=f_t_1694761137183__r_t_1694761137183__v_t_1694765579711__r_c_0; yfx_f_l_v_t_10003708=f_t_1691484399715__r_t_1694766217202__v_t_1694766217202__r_c_3; x_host_key=18a9832122f-4ef7376720bebdd3898423ce94d1248b2a62d4a1; yfx_c_g_u_id_10000080=_ck23091809312213491357557643035; Hm_lvt_c679af29b4d22a8f7077cabf4205f7ea=1695000683; yfx_f_l_v_t_10003714=f_t_1694679140062__r_t_1695002727931__v_t_1695015554324__r_c_2; yfx_f_l_v_t_10003717=f_t_1689926600306__r_t_1695016678767__v_t_1695016678767__r_c_2; yfx_f_l_v_t_10003701=f_t_1683697465811__r_t_1695002724628__v_t_1695016682240__r_c_7; yfx_c_g_u_id_10003718=_ck23091814015718531179687571523; yfx_f_l_v_t_10003718=f_t_1695016917853__r_t_1695016917853__v_t_1695016917853__r_c_0; yfx_c_g_u_id_10003711=_ck23091815114411558321759154357; yfx_f_l_v_t_10003711=f_t_1695021104155__r_t_1695021104155__v_t_1695021104155__r_c_0; JSESSIONID=_BmrTSGKStqSxtIUr-ZX8NiGpxrN9YOyobL-1RoXtYvc-cR_809j\\u00211913869236; yfx_f_l_v_t_10000080=f_t_1695000682324__r_t_1695091076738__v_t_1695091076738__r_c_1; Hm_lpvt_c679af29b4d22a8f7077cabf4205f7ea=1695091077; arp_scroll_position=305",
        #             "Origin": "https://shenzhen.chinatax.gov.cn",
        #             "Referer": f"https://shenzhen.chinatax.gov.cn/sztaxapp/zdsswfaj/list?time={params['time']}&type=w_date&msg={params['msg']}&token={params['token']}",
        #             "Sec-Fetch-Dest": "iframe",
        #             "Sec-Fetch-Mode": "navigate",
        #             "Sec-Fetch-Site": "same-origin",
        #             "Sec-Fetch-User": "?1",
        #             "Upgrade-Insecure-Requests": "1",
        #             "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36",
        #             "sec-ch-ua": '"Chromium";v="116", "Not)A;Brand";v="24", "Google Chrome";v="116"',
        #             "sec-ch-ua-mobile": "?0",
        #             "sec-ch-ua-platform": '"Windows"',
        #         }
        #         yield self.Request(
        #             url, params=params, data=data, callback=self.parse, year=request.year, headers=headers_page
        #         )

    def parse(self, response, **kwargs):
        li_list = response.xpath('//table[@id="table"]//input')
        # self.page_token = re.findall(".value='(.*?)';", response.text)[0]  # 翻页token
        # for li in li_list:
        #     url = 'https://shenzhen.chinatax.gov.cn' +\
        #           re.findall(r"show\('(.*?)'\)", li.xpath("./@onclick").get())[0]
        #     yield self.Request(url, callback=self.parse_detail)

    def parse_detail(self, response, **kwargs):
        if response.status_code == 404:
            return
        info = response.xpath('//table[@class="tab1"]//tr').re(r"<td.*?>\s*(.*?)\s*</td>")
        item = NetTaxIllegalProItem()
        item.province = self.province

        for k, *v in info:
            v = " ".join(v)
            k = re.sub(r"[^\u4e00-\u9fa5]+", "", k)
            if "纳税人名称" in k:
                v = re.sub(r"(<!--.*?-->|<.*?>|\s*)", "", v, flags=re.S)
                item.company_name = v
            if "纳税人识别号" in k:
                v = re.sub(r"(<!--.*?-->|<.*?>|\s*)", "", v, flags=re.S)
                item.taxpayer_id = v
            if "注册地址" in k:
                v = re.sub(r"(<!--.*?-->|<.*?>|\s*)", "", v, flags=re.S)
                item.city = item.resolve_register_address(v)[0]
                item.country = item.resolve_register_address(v)[1]
                item.address = v
            if "法定代表人或负责人姓名性别证件名称及号码" in k:
                v = re.sub(r"(<!--.*?-->|<.*?>|\s*)", "", v, flags=re.S)
                item.legal_representative = v
            if "案件性质" in k:
                v = " ".join(re.sub(r"(<!--.*?-->|<.*?>)", "", v, flags=re.S).split())
                item.illegal_status = v
            if "主要违法事实" in k:
                v = re.sub(r"(<!--.*?-->|\s*|<.*?>)", "", v, flags=re.S)
                illegal_facts_src = re.sub(r"(<.*?>|\s*|“|”)", "", re.sub(r"(<!--.*?-->)", "", v))
                item.illegal_facts = illegal_facts_src
            if "相关法律依据及税务处理处罚情况" in k:
                v = " ".join(re.sub(r"(<!--.*?-->|<.*?>)", "", v, flags=re.S).split()).replace('"', "")
                item.basis_and_punishment = v

        yield item


if __name__ == "__main__":

    from scrapy import cmdline

    cmdline.execute(argv=["scrapy", "crawl", "shenzhen_illegal"])
